import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('../views/list.vue')
  },
  {
    path: '/listabout',
    name: 'listabout',
    component: () => import('../views/listabout')
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫 to 去的页面 from 从哪里离开 next 放行
router.beforeEach((to, from, next) => {
  // 判断是不是登录页 如果是就放行 （白名单）如果想不登录 就进入就在判断后加to.path == '可以去的地址'
  if (to.path == '/'||to.path == '/list') {
    return next();
  }
  // 定义一个值为 本地存储的token 如果不等于定义的值就放行到当前页面
  const tokens = sessionStorage.getItem('token')
  if (!tokens) {
    return next('/')
  }
  next()
})
export default router
